<?php
namespace Home\Controller;
use Think\Controller;
class  BookController extends Controller{
    public function book_list(){
        $cat_id=isset($_POST['cat_id'])?trim($_POST['cat_id']):null;
        if(empty($cat_id)){
            $requset=array('returnCode'=>999,'returnMsg'=>'所请求数据为空','returnData'=>null);
            echo json_encode($requset); exit;
        }else{
            $book_list=M('book')->where(['cata_id'=>$cat_id])->select();
            if(empty($book_list)){
                $requset=array('returnCode'=>999,'returnMsg'=>'空数据','returnData'=>null);
                echo json_encode($requset); exit; 
            }else{
                  for($i=0;$i<count($book_list);$i++){
                       $book_list[$i]['share_price']=$book_list[$i]['price']-$book_list[$i]['minus'];
                       $book_list[$i]['cover']=C('domain').$book_list[$i]['cover'];
                       $result[$i]['id']=$book_list[$i]['id'];
                       $result[$i]['name']=$book_list[$i]['name'];
                       $result[$i]['cover']=$book_list[$i]['cover'];
                       $result[$i]['price']=$book_list[$i]['price'];
                       $result[$i]['share_price']=$book_list[$i]['share_price'];
                       $result[$i]['number']=$book_list[$i]['number'];
                  }
                $requset=array('returnCode'=>0,'returnMsg'=>'返回成功','returnData'=>$result);
                echo json_encode($requset); exit;
            }
        }
    }
    public function book_detail(){
        $cat_id=isset($_POST['cat_id'])?trim($_POST['cat_id']):null;
        $id=isset($_POST['book_id'])?trim($_POST['book_id']):null;
        if(empty($cat_id)||empty($id)){
            $requset=array('returnCode'=>999,'returnMsg'=>'所请求数据为空','returnData'=>null);
            echo json_encode($requset); exit;
        }else{
            $book_list=M('book')->where(['cata_id'=>$cat_id,'id'=>$id])->select();
            if(empty($book_list)){
                $requset=array('returnCode'=>999,'returnMsg'=>'空数据','returnData'=>null);
                echo json_encode($requset); exit;
            }else{
                for($i=0;$i<count($book_list);$i++){
                    $book_list[$i]['share_price']=$book_list[$i]['price']-$book_list[$i]['minus'];
                    $book_list[$i]['cover']=C('domain').$book_list[$i]['cover'];
                    $result[$i]['name']=$book_list[$i]['name'];
                    $result[$i]['cover']=$book_list[$i]['cover'];
                    $result[$i]['about']=$book_list[$i]['about'];
                    $result[$i]['price']=$book_list[$i]['price'];
                    $result[$i]['share_price']=$book_list[$i]['share_price'];
                    $result[$i]['number']=$book_list[$i]['number'];
                }
                $requset=array('returnCode'=>0,'returnMsg'=>'返回成功','returnData'=>$result);
                echo json_encode($requset); exit;
            }
        }
    }
}